package com.monitor.app_monitor.service.quartz.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.monitor.app_monitor.config.ClickHouseConfig;
import com.monitor.app_monitor.entity.EmsInfo;
import com.monitor.app_monitor.entity.TableList;
import com.monitor.app_monitor.entity.TaskLogAnnotation;
import com.monitor.app_monitor.entity.mapper.RealTimeAlarmClickhouseMapper;
import com.monitor.app_monitor.entity.mapper.ResUnifClickhouseMapper;
import com.monitor.app_monitor.mapper.EmsInfoMapper;
import com.monitor.app_monitor.mapper.TableListMapper;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassName dataNbCheck.java
 * @Author ning
 * @Description TODO
 * @CreateTime 2021年12月13日 17:11:00
 */


@DisallowConcurrentExecution //作业不并发
@Component
@Slf4j
public class DataNbCheckByEmsIp implements Job {

    @Resource
    private ClickHouseConfig clickHouseConfig;

    @Resource
    @Qualifier("tableByEmsIpMap")
    private Map<String, AtomicInteger> tableByEmsIpMap;

    @Autowired
    EmsInfoMapper emsInfoMapper;

    @Autowired
    TableListMapper tableListMapper;

    @Autowired
    RealTimeAlarmClickhouseMapper realTimeAlarmClickhouseMapper;

    @Autowired
    ResUnifClickhouseMapper resUnifClickhouseMapper;

    @TaskLogAnnotation
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

    /**
     * 分网管监控一干数据
     */

        List<EmsInfo> emsInfos = emsInfoMapper.selectList(new QueryWrapper<>(new EmsInfo()));
        List<TableList> tableLists = tableListMapper.selectList(new LambdaQueryWrapper<TableList>()
                .eq(TableList::getTrsType,"资源")
                .or()
                .eq(TableList::getTrsType,"拼接结果"));

        tableLists.forEach(tableList -> {
            try {
                resUnifClickhouseMapper.optimizeTable(tableList.getTableName()+"_local");
            } catch (Exception e) {
                e.printStackTrace();
            }
            log.info("执行{}表合并",tableList.getTableName());
            switch (tableList.getTableName()) {
                case "trs_channel":
                    //通道路由
                    emsInfos.forEach(emsInfo -> {
                        Integer allClick = resUnifClickhouseMapper.getTrsChannelNo(emsInfo.getEmsIp());
                        tableByEmsIpMap.get(tableList.getTrsDatabase() + "--" + tableList.getTableName() + emsInfo.getEmsIp()).set(allClick);
                    });

                    break;
                case "trs_channel_route":
                    emsInfos.forEach(emsInfo -> {
                        Integer allClick = resUnifClickhouseMapper.getTrsChannelRoute(emsInfo.getEmsIp());
                        tableByEmsIpMap.get(tableList.getTrsDatabase() + "--" + tableList.getTableName() + emsInfo.getEmsIp()).set(allClick);
                    });
                    break;
                case "trs_circuit":
                    emsInfos.forEach(emsInfo -> {
                        Integer allClick = resUnifClickhouseMapper.getTrsCircuit(emsInfo.getEmsIp());
                        tableByEmsIpMap.get(tableList.getTrsDatabase() + "--" + tableList.getTableName() + emsInfo.getEmsIp()).set(allClick);
                    });
                    break;
                case "trs_circuit_route":
                    emsInfos.forEach(emsInfo -> {
                        Integer allClick = resUnifClickhouseMapper.getTrsCircuitRoute(emsInfo.getEmsIp());
                        tableByEmsIpMap.get(tableList.getTrsDatabase() + "--" + tableList.getTableName() + emsInfo.getEmsIp()).set(allClick);
                    });
                    break;
                case "trs_seg":
                    emsInfos.forEach(emsInfo -> {
                        Integer allClick = resUnifClickhouseMapper.getTrsSeg(emsInfo.getEmsIp());
                        tableByEmsIpMap.get(tableList.getTrsDatabase() + "--" + tableList.getTableName() + emsInfo.getEmsIp()).set(allClick);
                    });
                    break;
                case "trs_seg_route":
                    emsInfos.forEach(emsInfo -> {
                        Integer allClick = resUnifClickhouseMapper.getTrsSegRoute(emsInfo.getEmsIp());
                        tableByEmsIpMap.get(tableList.getTrsDatabase() + "--" + tableList.getTableName() + emsInfo.getEmsIp()).set(allClick);
                    });
                    break;
                default:
                    //资源
                    emsInfos.forEach(emsInfo -> {
                        Integer allClick = resUnifClickhouseMapper.findAllClick(tableList.getTableName(), emsInfo.getEmsIp());
                        tableByEmsIpMap.get(tableList.getTrsDatabase() + "--" + tableList.getTableName() + emsInfo.getEmsIp()).set(allClick);
                    });
                    break;
            }

        });

    }
}
